


//一个字符转为二进制
function toBinary(val){
    if(typeof val === 'number'){
        return parseInt(val, 10).toString(2)
    }else if(typeof val === 'string' ){
        return val.charCodeAt().toString(2)
    }else {
        return null
    }

}

var str = 'I'
var num = 2

console.log(toBinary(str)); // 1001001
console.log(toBinary(num)); // 10



//传入二进制字符串，翻译成英语句子并返回
function binaryAgent(str) {
    var result = ''
    var arr =str.split(" ")

    for(var i=0;i<arr.length;i++){
        result +=String.fromCharCode(parseInt(arr[i], 2))
    }
    return result;
}

var bStr = "01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"
console.log(binaryAgent(bStr)); //Aren't bonfires fun!?

